//
//  AroundHotelViewController.m
//  SXH
//
//  Created by 龚啸 on 15/9/16.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "AroundHotelViewController.h"
#import "CommonService.h"
#import "UIImageView+WebCache.h"
#import "HotelEntity.h"
#import "UIImageView+WebCache.h"
#import "RouteSearchDemoViewController.h"
#import "SXH-swift.h"
#import "Colours.h"
#import "UIView+AutoLayout.h"
#import "LocationManagement.h"

@interface AroundHotelViewController ()<UIWebViewDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *btnPhone;
@property (weak, nonatomic) IBOutlet UIWebView *webViewContent;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollViewHotel;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIImageView *s_photo_url;
@property (weak, nonatomic) IBOutlet UILabel *product_score;
@property (weak, nonatomic) IBOutlet UILabel *decoration_score;
@property (weak, nonatomic) IBOutlet UILabel *service_score;
@property (weak, nonatomic) IBOutlet UILabel *address;
@property (weak, nonatomic) IBOutlet UILabel *telephone;
@property (weak, nonatomic) IBOutlet UILabel *navTitle;
@property (weak, nonatomic) IBOutlet UILabel *avg_price;
@property (weak, nonatomic) IBOutlet UIImageView *rating_s_img_url;
@property (weak, nonatomic) IBOutlet UIView *viewTag;

@property(nonatomic, strong) RatingBar *myRating;
@property(nonatomic, strong) TagListView *myTagList;
@property (strong, nonatomic) HotelEntity *hotelEntity;

@end

@implementation AroundHotelViewController
@synthesize webViewContent,hotelEntity,scrollViewHotel,parkEntity;
@synthesize strTitle;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    webViewContent.delegate = self;
    self.navTitle.text = strTitle;
    
//    [self getHotelDataWithPage:self.strPageNo limit:@"20" parkid:parkEntity.ID  category:self.index   business_id:self.businessid];
    
    
    [self getHotelDataWithbusiness_id:self.businessid latitude:self.latitude longitude:self.longitude ];
    webViewContent.frame =CGRectMake(0, 228, ScreenWidth, webViewContent.bounds.size.height);
    [scrollViewHotel addSubview:webViewContent];
   
    DebugLog(@"%@",self.businessid);
    
    webViewContent.scrollView.scrollEnabled = NO;
  
    
}
- (IBAction)shareAction:(id)sender {

    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.delegate = self;
    HUD.labelText = Loading_Hard_Text;
    
    NSArray* imageArray = [[NSArray alloc] initWithObjects:[self.s_photo_url image], nil];
    
    [CommonService showShareActionSheet:self.view HUD:HUD
                             ImageArray:imageArray
                                    URL:PORTAL_URL
                                  Title:parkEntity.title
                                Content:parkEntity.content];
}

- (void)getHotelDataWithbusiness_id:(NSString *)business_id
                           latitude:(NSString *)latitude
                          longitude:(NSString *)longitude{
    [super showLoadingView:YES];
    CommonService *service = [[CommonService alloc] init];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           business_id,@"business_id",
                           latitude,@"latitude",longitude,@"longitude",nil];
        [service getNetWorkData:param
                       Path:BUSINESSES
                  Successed:^(id entity) {
                      [super showLoadingView:NO];
                      NSDictionary *dic = (NSDictionary *)entity;

                      NSArray *rows = [dic valueForKeyPath:@"rows"];
                      
                      NSDictionary *dic2 =[rows objectAtIndex:0];
                    
                      
                      hotelEntity =[[HotelEntity alloc]initWithAttributes:dic2];

                      
                      if ([hotelEntity.branch_name isEqualToString:@"(无)"]) {
                          self.name.text = hotelEntity.name ;
                      } else {
                          self.name.text = [NSString stringWithFormat:@"%@%@",hotelEntity.name,hotelEntity.branch_name];
                      }
                     
                      
                      if (hotelEntity.avg_price) {
                          self.avg_price.text = [NSString stringWithFormat:@"￥%@人",hotelEntity.avg_price];
                      }
                      self.product_score.text = [NSString stringWithFormat:@"房间： %@",hotelEntity.product_score];
                      self.decoration_score.text = [NSString stringWithFormat:@"环境： %@",hotelEntity.decoration_score];
                      self.service_score.text = [NSString stringWithFormat:@"服务： %@",hotelEntity.service_score];
                      self.address.text =hotelEntity.address;
                      self.telephone.text = hotelEntity.telephone;
                      [self.s_photo_url sd_setImageWithURL:[NSURL URLWithString:hotelEntity.photo_url]];
                      DebugLog(@"%@",self.s_photo_url);
                      
                      self.myRating = [[RatingBar alloc]initWithFrame:CGRectMake(0, 0, 90, 20)];
                      self.myRating.rating = [hotelEntity.avg_rating doubleValue];
                      [self.rating_s_img_url addSubview:self.myRating];
                      
                      //webView
                      
                      NSString * urlString = hotelEntity.introduction_url;
                      NSURL *url = [NSURL URLWithString:urlString];
//                      NSData *data = [NSData dataWithContentsOfURL:url];
//                      NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//                      NSString * str2 =@"<div id=\"content\" contenteditable=\"false\">";
//                      NSString * str3 = [str2 stringByAppendingString:str];
//                      NSString *str4 = [str3 stringByAppendingString:@"</div>"];
//                      DebugLog(@"string===============%@",str4);
//                      [webViewContent loadHTMLString:str4 baseURL:nil];
                      NSURLRequest *request = [NSURLRequest requestWithURL:url];
                      [webViewContent loadRequest:request];
                      
                      self.myTagList = [TagListView autoLayoutView];
                      
                      NSString *has_coupon = hotelEntity.has_coupon;
                      if ([has_coupon intValue]) {
                          //[cell.myTagList addTag:@"惠"];
                          [self.myTagList addTag2:@"惠" colour:[UIColor carrotColor]];
                      }
                      
                      NSString *has_deal = hotelEntity.has_deal;
                      if ([has_deal intValue]) {
                          //[cell.myTagList addTag:@"团"];
                          [self.myTagList addTag2:@"团" colour:[UIColor cantaloupeColor]];
                      }
                      
                      NSString *has_online_reservation = hotelEntity.has_online_reservation;
                      if ([has_online_reservation intValue]) {
                          //[cell.myTagList addTag:@"订"];
                          [self.myTagList addTag2:@"订" colour:[UIColor seafoamColor]];
                      }
                      
                      [self.viewTag addSubview:self.myTagList];
                      
                      [self.myTagList pinToSuperviewEdges:JRTViewPinTopEdge inset:5.0];
                      [self.myTagList pinToSuperviewEdges:JRTViewPinRightEdge inset:5.0];
                      UIFont *font = [UIFont systemFontOfSize:12.0];
                      
                      [self.myTagList constrainToMinimumSize:CGSizeMake(font.pointSize+4, font.pointSize+4)];
                      
                      [self.myTagList pinAttribute:NSLayoutAttributeLeading toAttribute:NSLayoutAttributeTrailing ofItem:self.name withConstant:5.0];
                      
                      dispatch_async(dispatch_get_main_queue(), ^{
                          
                      });
                  } Failed:^(NSInteger errorCode, NSString *message) {
                      NSLog(@"===");
                      [super showLoadingView:NO];
                  }];
}

- (IBAction)doCallPhone:(id)sender {
    NSString *number = self.btnPhone.text;// 此处读入电话号码
    
    // NSString *num = [[NSString alloc] initWithFormat:@"tel://%@",number]; //number为号码字符串 如果使用这个方法 结束电话之后会进入联系人列表
    
    NSString *num = [[NSString alloc] initWithFormat:@"telprompt://%@",number]; //而这个方法则打电话前先弹框  是否打电话 然后打完电话之后回到程序中 网上说这个方法可能不合法 无法通过审核
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat documentHeight = [[webViewContent stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"] floatValue];
     CGRect frame = webViewContent.frame;
    DebugLog(@"document =======================%f",documentHeight);
    frame = CGRectMake(frame.origin.x,frame.origin.y, frame.size.width, documentHeight+20);
    webViewContent.frame = frame;
    DebugLog(@"webViewContent ========%f",webViewContent.frame.size.height);
    
    CGRect frame2 = scrollViewHotel.frame;
//    if(iPhone4s){
//        frame2.size.height = documentHeight + self.view.frame.size.height - webViewContent.frame.origin.y-5;
//    }else{
    frame2.size.height = documentHeight + webViewContent.frame.origin.y+20;
//    }
    scrollViewHotel.contentSize = frame2.size;
}

- (IBAction)addressClick:(id)sender {
    RouteSearchDemoViewController * vc = [[RouteSearchDemoViewController alloc]init];
    vc.strLat = hotelEntity.latitude;
    vc.strLon = hotelEntity.longitude;
    vc.strAddress = hotelEntity.address;
    vc.strTitle = hotelEntity.name;
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"AroundHotelViewController"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"AroundHotelViewController"];
}

@end
